//
//  RootTabBarController.m
//  Weibo
//
//  Created by 千锋 on 15/12/29.
//  Copyright © 2015年 abc. All rights reserved.
//

#import "RootTabBarController.h"
#import "HomeTableViewController.h"
#import "MessageTableViewController.h"
#import "DiscoveryTableViewController.h"
#import "PersonalTableViewController.h"
#import "WeboTabBar.h"
#import <PopMenu.h>

@interface RootTabBarController ()<WBTabBarDelegate>

@property(nonatomic,strong) WeboTabBar *wbTabBar;

//弹出界面
@property(nonatomic,strong) PopMenu *popMenu;

@end

@implementation RootTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    // 添加自定义TabBar
    [self addCustomTabBar];
    
    // 添加viewControllers
    [self addViewControllers];
    
}

-(PopMenu *)popMenu
{
    if (_popMenu == nil) {
        
        NSMutableArray *items = @[].mutableCopy;
        
        NSArray *titleArray = @[@"文字",@"相册",@"微博",@"签到",@"点评",@"更多"];
        NSArray *imgNameArr = @[@"tabbar_compose_idea",@"tabbar_compose_photo",@"tabbar_compose_weibo",@"tabbar_compose_lbs",@"tabbar_compose_review",@"tabbar_compose_more"];
        for (int i = 0; i < titleArray.count; i++) {
            MenuItem *item = [[MenuItem alloc]initWithTitle:titleArray[i] iconName:imgNameArr[i] glowColor:[UIColor magentaColor]];
            [items addObject:item];
        }
        
        _popMenu = [[PopMenu alloc]initWithFrame:[UIScreen mainScreen].bounds items:items];
        _popMenu.didSelectedItemCompletion = ^(MenuItem *item){
            NSLog(@"点击了--%@",item.title);
        };
        
    }
    return _popMenu;
}

// 添加自定义TabBar
-(void)addCustomTabBar{
    // 隐藏系统自带的
    self.tabBar.hidden = YES;
    self.wbTabBar = [[WeboTabBar alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49)];
    [self.view addSubview:self.wbTabBar];
#if 0
    self.wbTabBar.delegate = self;
#endif
    __weak typeof(self) weakSelf = self;
    self.wbTabBar.passIndex = ^(NSInteger index){
        weakSelf.selectedIndex = index;
    };
    self.wbTabBar.plubBlock = ^{
        // 显示弹出界面
        [weakSelf.popMenu showMenuAtView:weakSelf.view];
    };
//    self.wbTabBar.backgroundColor = [UIColor orangeColor];
    self.wbTabBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
    
}

// 添加视图控制器
-(void)addViewControllers{
    
    
    NSArray *vcNameArray = @[@"HomeTableViewController",@"MessageTableViewController",@"DiscoveryTableViewController",@"PersonalTableViewController"];
    NSArray *vctitleAttry = @[@"首页",@"消息",@"发现",@"我"];
    
    NSArray *norImageArray = @[@"tabbar_home",@"tabbar_message_center",@"tabbar_discover",@"tabbar_profile"];
//    NSArray *hlImageArray = @[@"tabbar_home_highlighted",@"tabbar_message_center_highlighted",@"tabbar_discover_highlighted",@"tabbar_profile_highlighted"];
    NSArray *selImageArray = @[@"tabbar_home_selected",@"tabbar_message_center_selected",@"tabbar_discover_selected",@"tabbar_profile_selected"];
    
    for (NSString *vcName in vcNameArray) {
        Class vcClass = NSClassFromString(vcName);
        NSInteger index = [vcNameArray indexOfObject:vcName];
        
        UITableViewController *vc = [[vcClass alloc]init];
        vc.title = vctitleAttry[index];
        
        [vc.tabBarItem setImage:[[UIImage imageNamed:norImageArray[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:selImageArray[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        
        // 修改文字颜色
        NSDictionary *norAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
        NSDictionary *selAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor orangeColor]};
        
        [vc.tabBarItem setTitleTextAttributes:norAttribute forState:UIControlStateNormal];
        [vc.tabBarItem setTitleTextAttributes:selAttribute forState:UIControlStateSelected];
        
        
        UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:vc];
        [self addChildViewController:navc];
        
        self.wbTabBar.tabBarItem = vc.tabBarItem;
        
    }
}

#if 0
#pragma mark - 
-(void)passIndex:(NSInteger)index
{
    self.selectedIndex = index;
}
#endif

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
